<template>
  <div>
    <router-link to="/">
      <button>跳转到Home</button>
    </router-link>
    <h4>组件内路由守卫</h4>
  </div>
</template>
<script>
export default {
  name: 'componentGuard',
  beforeRouteEnter(to, from, next) {
    if(to.meta.auth && !window.isLogin) { //需要验证且未登录
      if(window.confirm("请登录-组件内路由")) {
        window.isLogin = true
        next() //登录成功，前往目标页面
      } else {
        next('/') //放弃登录，返回首页
      }
    } else { //不需要验证或已登录
      next()
    }
  },
  data () {
    return {};
  },
  created() {
    window.isLogin = false;
  }
}
</script>